package com.chouxiaozi.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    @GetMapping("/testA")
    public String testA() {

        ContextUtil.enter("/testControl", "controlOrigin");
        System.out.println("/testA>>>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "------testA"+System.currentTimeMillis();
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }

    @GetMapping("/testRT")
    public String testRT(Integer time) {
        if(null != time){//sleep指定时间
            try {
                TimeUnit.SECONDS.sleep(time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("time:"+time+" ,/testRT>>>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "------testRT";
    }

    @GetMapping("/testRT2")
    public String testRT2(Integer num) {
        int i = 10/num;
        System.out.println("/testRT2>>>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return "------testRT2";
    }

    @GetMapping("/testParamHot")
    @SentinelResource(value = "testParamHot",blockHandler = "blockHandler_ParamHot")
    public String testParamHot(Integer param1, Integer param2) {
        return "------testParamHot";
    }

    public String blockHandler_ParamHot(Integer param1, Integer param2, BlockException e){
        System.out.println("e"+e);
        return "------blockHandler_ParamHot";
    }
}
